package Pong;

import java.net.*;
import java.io.*;

public class ServerThread extends Thread {
    
    private Socket socket = null;
    private int cnr;
    PrintWriter out;
    BufferedReader in;
    String fromClient;
    String toClient;
    Server server;
    int balx,baly;

    // Constructor
    public ServerThread(Socket socket,int cnr,Server server) {
	  this.server = server;
      this.socket = socket;         // koppel onze socket aan de opgegeven socket 
	  this.cnr = cnr + 1;           // verhoog het connectie-nummer
	  try {  // zelfde code als voor de KnockKnockServer voor 1 client
	    out = new PrintWriter(socket.getOutputStream(), true);
	    in = new BufferedReader(
				    new InputStreamReader(
				    socket.getInputStream()));
	  }
	  catch (IOException e) {
	    e.printStackTrace();
	  }
	  
    }

    public void run() {
	  
      try{
    	  boolean spelen = true;
    	  out.println("start");
    	  while (spelen) {

    		  ////////////////////Verzenden////////////////////

    		  out.println(toClient);
    		  
    		  ////////////////////Ontvangen////////////////////
    		  
    		  fromClient = in.readLine();
    		  if(fromClient.equals("stop")){
    			  spelen = false;
    		  }
    		  
    		  toClient = (cnr+" "+server.samenplakkenXpos());
    		slaap(10);
    	  }

	    out.close();
	    in.close();
	    socket.close();

	  } 
	  catch (IOException e) {
	    e.printStackTrace();
	  }
      
    }
    
    public String getClientXpos(){
    	return fromClient;
    }
    
    public static void slaap (int mSec){		 
  		try {
  			Thread.currentThread().sleep(mSec); 
  		} catch (InterruptedException e){ }
  	} 
}